package com.bite.bite_forum.exception;

import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ApplicationException.class)
    @ResponseBody
    public AppResult applicationHandler(ApplicationException e){
        e.printStackTrace();
        //打印日志
        log.info(e.getMessage());
        if(e.getErrorResult() != null){
            return e.getErrorResult();
        }
         //非空校验
        if(e.getMessage() == null || e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());

    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AppResult exceptionHandler(Exception e){
        e.printStackTrace();
        //打印日志
        log.info(e.getMessage());

        //非空校验
        if(e.getMessage() == null || e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());

    }

}
